package pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.ISources;
import org.eclipse.ui.handlers.HandlerUtil;

public abstract class AbstractSelectionHandler extends CommandStackCallbackHandler {

	// *** Attributes

	// *** Methods
	
	protected abstract Object execute(ExecutionEvent event, CommandStack stack, Object[] selection) 
			throws ExecutionException;
	
	@Override
	public Object execute(ExecutionEvent event, CommandStack stack) throws ExecutionException {
		Object selection = HandlerUtil.getVariable(event, ISources.ACTIVE_CURRENT_SELECTION_NAME);
		if (!(selection instanceof StructuredSelection)) return null;
		
		StructuredSelection structuredSelection = (StructuredSelection) selection;
		Object[] array = structuredSelection.toArray();
		return execute(event, stack, array);
	}

}
